iT邦幫忙

2021 iThome 鐵人賽

DAY 16
0
自我挑戰組

從雲端開始的菜鳥任務系列 第 16

Day 16 Jenkins & Kubernetes

  • 分享至 

  • xImage
  •  

Devops with Jenkins, Kubernetes

部屬 Jenkins

建立 namespace

sudo kubectl create namespace devops

建立 Jenkins hostpath 路徑

cd /
mkdir /jenkins-data
chmod -R 777 /jenkins-data

建立 service account

sa.yaml

apiVersion: v1
kind: ServiceAccount
metadata:
  name: jenkins-sa
  namespace: devops

---
apiVersion: rbac.authorization.k8s.io/v1beta1
kind: ClusterRole
metadata:
  name: jenkins-cr
rules:
  - apiGroups: ["extensions", "apps"]
    resources: ["deployments"]
    verbs: ["create", "delete", "get", "list", "watch", "patch", "update"]
  - apiGroups: [""]
    resources: ["services"]
    verbs: ["create", "delete", "get", "list", "watch", "patch", "update"]
  - apiGroups: [""]
    resources: ["pods"]
    verbs: ["create","delete","get","list","patch","update","watch"]
  - apiGroups: [""]
    resources: ["pods/exec"]
    verbs: ["create","delete","get","list","patch","update","watch"]
  - apiGroups: [""]
    resources: ["pods/log"]
    verbs: ["get","list","watch"]
  - apiGroups: [""]
    resources: ["secrets"]
    verbs: ["get"]

---
apiVersion: rbac.authorization.k8s.io/v1beta1
kind: ClusterRoleBinding
metadata:
  name: jenkins-crd
roleRef:
  kind: ClusterRole
  name: jenkins-cr
  apiGroup: rbac.authorization.k8s.io
subjects:
- kind: ServiceAccount
  name: jenkins-sa
  namespace: devops

建立 Jenkins 服務

jenkins-deploy.yaml

---
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: jenkins
  namespace: devops
spec:
  template:
    metadata:
      labels:
        app: jenkins
    spec:
      terminationGracePeriodSeconds: 10
      serviceAccount: jenkins-sa
      containers:
      - name: jenkins
        image: jenkins/jenkins:lts
        imagePullPolicy: IfNotPresent
        ports:
        - containerPort: 8080
          name: web
          protocol: TCP
        - containerPort: 50000
          name: agent
          protocol: TCP
        volumeMounts:
        - name: jenkinshome
          mountPath: /var/jenkins_home
      securityContext:
        fsGroup: 1000
      volumes:
      - name: jenkinshome
        hostPath:
          path: /jenkins-data
          type: Directory

---
apiVersion: v1
kind: Service
metadata:
  name: jenkins
  namespace: devops
  labels:
    app: jenkins
spec:
  selector:
    app: jenkins
  type: NodePort
  ports:
  - name: web
    port: 8080
    targetPort: web
    nodePort: 30002
  - name: agent
    port: 50000
    targetPort: agent

建立 pvc (本次無使用)

pvc.yaml

---
apiVersion: v1
kind: PersistentVolume
metadata:
  name: jenkins-pv
spec:
  capacity:
    storage: 5Gi
  accessModes:
  - ReadWriteMany
  persistentVolumeReclaimPolicy: Delete
  nfs:
    server: 172.16.1.128
    path: /data/k8s/jenkins

---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: jenkins-pvc
  namespace: devops
spec:
  accessModes:
  - ReadWriteMany
  resources:
    requests:
      storage: 5Gi

查看服務

kubectl get deploy -A
NAMESPACE     NAME      READY   UP-TO-DATE   AVAILABLE   AGE
devops        jenkins   1/1     1            1           5m48s
kube-system   coredns   2/2     2            2           21m
kubectl get pods -A
NAMESPACE     NAME                                READY   STATUS    RESTARTS   AGE
devops        jenkins-76d7f5b59d-8cpbl            1/1     Running   0          6s
kube-system   coredns-5c98db65d4-2pn7j            1/1     Running   0          15m
kube-system   coredns-5c98db65d4-b9b59            1/1     Running   0          15m
kube-system   etcd-mgchung-3                      1/1     Running   0          14m
kube-system   kube-apiserver-mgchung-3            1/1     Running   0          14m
kube-system   kube-controller-manager-mgchung-3   1/1     Running   0          14m
kube-system   kube-flannel-ds-amd64-2s7bt         1/1     Running   0          15m
kube-system   kube-proxy-f2vmd                    1/1     Running   0          15m
kube-system   kube-scheduler-mgchung-3            1/1     Running   0          14m
kubectl get svc -A
NAMESPACE     NAME         TYPE        CLUSTER-IP       EXTERNAL-IP   PORT(S)                          AGE
default       kubernetes   ClusterIP   10.96.0.1        <none>        443/TCP                          19m
devops        jenkins      NodePort    10.101.209.110   <none>        8080:30002/TCP,50000:30103/TCP   4m14s
kube-system   kube-dns     ClusterIP   10.96.0.10       <none>        53/UDP,53/TCP,9153/TCP           19m

登入 Jenkins

先 log Jenkins 的 pod 查看密碼

sudo kubectl get pods -n jenkins
sudo kubectl logs <pod_name> -n jenkins

就會看到

Jenkins initial setup is required. An admin user has been created and a password generated.
Please use the following password to proceed to installation:

c3a14ac90ed0481988e5845fb422815b

This may also be found at: /var/jenkins_home/secrets/initialAdminPassword

使用瀏覽器輸入

<ip-address>:30002

將 log 到的密碼輸入頁面,進行 plugin

利用 Jenkins 部屬服務到 Kubernetes

  • 左邊工具列 -> 外掛程式管理 ->搜尋 Kubernetes
  • 勾選 Kubernetes 和 Kubernetes Continous Deploy 下載安裝


上一篇
Day 15 K3S & Raspberry Pi
下一篇
Day 17 利用 helm 安裝 Jenkins
系列文
從雲端開始的菜鳥任務30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言